package com.example.administrator.mpandroidchart;

import android.app.Activity;
import android.content.Context;

import com.example.administrator.mpandroidchart.listener.IPresenter;
import com.example.administrator.mpandroidchart.listener.IView;
import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017-06-13.
 */

public class Presenter implements IPresenter {
    MainActivity v;
    int hchartX = 0,hcontY = 0;
    int bchartX = 0,bcontY = 0;
    boolean DataFlag = false;
    int[] hdataY = new int[18];
    int[] bdataY = new int[5];

    private Thread hrefurbishData = new Thread() {
        @Override
        public void run() {
            super.run();
            while (true) {
                //刷新数据
                try {
                    sleep(500);
                    v.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            hupData();
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    private Thread brefurbishData = new Thread() {
        @Override
        public void run() {
            super.run();
            while (true) {
                //刷新数据
                try {
                    sleep(500);
                    v.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            bupData();
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    /**
     * 更新心率数据
     * */
    private void hupData() {
        Entry entry;
        hchartX++;

        if(DataFlag){
            v.hreceiveData( new Entry(hchartX,hdataY[hcontY]));
            hcontY++;
            if(hcontY == 18){
                hcontY = 0;
//                DataFlag = false;
            }
        }else{
            v.hreceiveData( new Entry(hchartX,6));
        }
    }
    /**
     * 更新呼吸数据
     * */
    private void bupData() {
        Entry entry;
        bchartX += 2;

        if(DataFlag){
            v.breceiveData( new Entry(bchartX,bdataY[bcontY]));
            bcontY++;
            if(bcontY == 5){
                bcontY = 0;
//                DataFlag = false;
            }
        }else{
            v.breceiveData( new Entry(bchartX,6));
        }
    }

    @Override
    public void attachV(MainActivity v) {
        this.v = v;
        hrefurbishData.start();
        brefurbishData.start();
    }

    /**
     * 在这里接收外界传入View的数据,
     */
    @Override
    public void request() {
        DataFlag = true;
        //在这里将需要放入图表的Y轴值放入dataY数组里面
        hdataY[0] = 6;
        hdataY[1] = 9;
        hdataY[2] = 9;
        hdataY[3] = 6;
        hdataY[4] = 6;
        hdataY[5] = 6;
        hdataY[6] = 6;
        hdataY[7] = 6;
        hdataY[8] = 7;
        hdataY[9] = 6;
        hdataY[10] = 6;
        hdataY[11] = 6;
        hdataY[12] = 8;
        hdataY[13] = 6;
        hdataY[14] = 6;
        hdataY[15] = 6;
        hdataY[16] = 16;
        hdataY[17] = 5;

        bdataY[0] = 15;
        bdataY[1] = 15;
        bdataY[2] = 16;
        bdataY[3] = 16;
        bdataY[4] = 17;
//        bdataY[5] = 17;
//        bdataY[6] = 18;
//        bdataY[7] = 18;
//        bdataY[8] = 19;
//        bdataY[9] = 19;
//        bdataY[10] = 20;
//        bdataY[11] = 20;
//        bdataY[12] = 21;
//        bdataY[13] = 21;
//        bdataY[14] = 22;
//        bdataY[15] = 22;
//        bdataY[16] = 23;
//        bdataY[17] = 23;
//        bdataY[18] = 24;
//        bdataY[19] = 24;
//        bdataY[20] = 25;
//        bdataY[21] = 25;
//        bdataY[22] = 26;
//        bdataY[23] = 26;
//        bdataY[24] = 27;
//        bdataY[25] = 27;
//        bdataY[26] = 28;
//        bdataY[27] = 28;
//        bdataY[28] = 29;
//        bdataY[29] = 29;
//        bdataY[30] = 30;
//        bdataY[31] = 30;
//        bdataY[32] = 29;
//        bdataY[33] = 29;
//        bdataY[34] = 28;
//        bdataY[35] = 28;
//        bdataY[36] = 27;
//        bdataY[37] = 27;
//        bdataY[38] = 26;
//        bdataY[39] = 26;

    }

}
